package org.arclib.x.gui.widgets;

import java.awt.event.MouseEvent;

import org.apache.log4j.Logger;
import org.arclib.Font;
import org.arclib.draw.Color;
import org.arclib.math.Collision;
import org.arclib.math.Point;
import org.arclib.math.Size;
import org.lwjgl.input.Mouse;

public class Widget
{
	/// set name of the widget 
	void setSize(Size argSize)
	{
		size = argSize;
	}

	/// set width of widget
	void setWidth(double argW)
	{
		size.w = argW; 
	}

	/// set height of widget
	void setHeight(double argH)
	{
		size.h = argH; 
	}

	/// set parent position of sprite 
	void setPosition(Point argPos)
	{
		position = argPos;
	}

	/// set color of the widget 
	void setColor(Color acolor)
	{
		color = acolor;
	}

	/// process widget 
	void process(Point parentPos)
	{
		boolean mOver = isMouseOver(parentPos); 

		action = ActionType.DEFAULT;
	
		if (mOver && Mouse.isButtonDown(MouseEvent.BUTTON1))
		{
			//action = ActionType.CLICKON;
			//clickOn();

			if (Mouse.isButtonDown(MouseEvent.BUTTON1))
			{
				focus = true; 
			}
		}
		else if (Mouse.isButtonDown(MouseEvent.BUTTON1))
		{
			// mouse button is clicked not on this widget
			//clickOff(); 
			//action = ActionType.CLICKOFF;

			if (Mouse.isButtonDown(MouseEvent.BUTTON1))
			{
				focus = false; 
			}
		}
		
		if (mOver)
		{
			// call mouse over signal
			//mouseOver();
			action = ActionType.MOUSEOVER; 
		}
		
	}

	/// get width 
	double getWidth() {  return size.w; }

	/// get height 
	double getHeight() { return size.h; } 
	
	/// get size
	Size getSize() { return size; }

	/// get x position 
	double getX() { return position.x; }

	/// get Y position 
	double getY() { return position.y; }
	
	/// get position
	Point getPosition() { return position; }

	/// return true if mouse is over widget 
	boolean isMouseOver(Point parentPos) 
	{ 
		return Collision.boxXYCollision(new Point(Mouse.getX(), Mouse.getY()), position.add(parentPos), size); 
	} 

	/// set color of font 
	void setFontColor(Color afontColor)
	{
		fontColor = afontColor;
	}

	/// set text values
	void setText(String argText)
	{
		text = argText; 
	}

	/// set font value 
	void setFont(Font argFont)
	{
		font = argFont; 
		fontAlign = getAlignment(); 
	}

	/// draw image from position + parent position  
	void draw(Point parentPos)
	{
		log.fatal("Base Widget Class Draw Functionality Not Implemented"); 
	}

	/// set maximum amount of lines 
	void setMaxLines(int argLines)
	{
		log.fatal("Base Widget Class setMaxLines Functionality Not Implemented"); 
	}

	/// set maximum width  
	void setMaxWidth(int maxWidth)
	{
		log.fatal("Base Widget Class setMaxWidth Functionality Not Implemented"); 
	}

	/// return focus 
	boolean getFocus() { return focus; }

	/// set alignment of text
	void setAlignment(Alignment aText)
	{
		alignment = aText; 
		fontAlign = getAlignment(); 
	}
	
	/// get text inside of widget
	String getText() { return text; }
	
	/// called when widget is clicked on 
	//Signal!() clickOn;
	
	/// called when widget is clicked off of 
	//Signal!() clickOff; 
	
	/// called when mouse is over widget
	//Signal!() mouseOver; 
	
	// get alignment 	
	protected Point getAlignment()
	{
		Point drawFontPos = Point.Zero;
		
		switch(alignment)
		{
			case LEFTCENTER: 
			drawFontPos = position;
			drawFontPos.y += size.h/2 - font.getHeight()/2; 
			break; 
			
			case LEFTUP: 
			drawFontPos = position;
			break; 
			
			case LEFTDOWN: 
			drawFontPos = position;
			drawFontPos.y += size.h - font.getHeight(); 
			break; 
			
			case RIGHTCENTER: 
			drawFontPos = position;
			drawFontPos.y += size.h/2 - font.getHeight()/2;
			drawFontPos.x += size.w - font.getWidth(text); 			
			break;

			case RIGHTUP: 
			drawFontPos = position;
			drawFontPos.x += size.w - font.getWidth(text); 			
			break;

			case RIGHTDOWN: 
			drawFontPos = position;
			drawFontPos.y += size.h - font.getHeight();
			drawFontPos.x += size.w - font.getWidth(text); 			
			break;
			
			case CENTER: 
			drawFontPos = position.add(size.div(2).sub(new Point(font.getWidth(text), font.getHeight()).div(2)));
			break; 
			
			case CENTERUP: 
			drawFontPos = position;
			drawFontPos.x += size.w/2 - font.getWidth(text)/2; 
			break; 
			
			case CENTERDOWN: 
			drawFontPos = position;
			drawFontPos.x += size.w/2 - font.getWidth(text)/2;
			drawFontPos.y += size.h - font.getHeight();
			break; 
		}

		log.fatal("getAlignment() - could not position Widget"); 
		return drawFontPos; 
	}
	  
	// will hold the position relative to the layout
	Point position;
	Size size;

	// color values 
	Color color;

	// font color values 
	Color fontColor; 

	// font text and alignment 
	String text; 
	Alignment alignment = Alignment.CENTER; 

	// font 
	Font font; 
	Point fontAlign; 

	// focus value 
	boolean focus=false;

	// info signal will emit
	ActionType action;  
	
	private final static Logger log = Logger.getLogger(Widget.class);
}
